﻿using AspectCore.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Finbook.ConsoleApp.Interceptors
{
    public class HelloFallbackInterceptorAttribute
        : AbstractInterceptorAttribute
    {
        public string FallbackMethod { get; set; }
        public HelloFallbackInterceptorAttribute(string method)
        {
            FallbackMethod = method;
        }
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                var method = context.ServiceMethod.DeclaringType.GetMethod(FallbackMethod);
                var rt = method.Invoke(context.Implementation, context.Parameters);
                context.ReturnValue = rt;
                throw ex;
            }
        }
    }
}
